import sys
from dotenv import dotenv_values
env_vars = dotenv_values(".env")
sys.path.append(env_vars["DEFAULT_PATH"])
from app.utils.async_db import AsyncDbUtils,User

class AsyncDao(object):
    Model = None

    def __init__(self, user_id=0):
        self.user_id = user_id
        self.db = AsyncDbUtils()
    async def create(self, model: Model):
            """
        创建一条数据
        :param model: 数据模型实例
        """
            async with self.db.sess() as session:
                session.add(model)
                await session.commit()
                session.expunge(model)
                # print(res.id)
            # await self.db.engine.dispose()
                await session.flush()
    async def read(self, id: int) -> Model:
            """
        通过id读取一条数据
        :param id: 数据id
        :return: 数据模型实例
        """
            async with self.db.sess() as session:
                model = await session.get(self.Model, id)
                await session.flush()
            return model

# async def main():
#     dao=AsyncDao()
#     await dao.create(model=User(name='测试',avatar='123'))
# if __name__ == '__main__':
#     import asyncio
#     asyncio.run(main())
    